local skel = fk.CreateSkill {
  name = "lb__xingsong",
}

local DIY = require "packages.diy_utility.diy_utility"

Fk:loadTranslationTable {
  ["lb__xingsong"] = "星颂",
  --[":lb__xingsong"] = "每轮开始时，你可以明置一张手牌且此牌不可被弃置。其他角色使用牌后，你可以将一张同名「<b>星颂</b>」牌交给其，然后你与其各摸一张牌。",
  --8.30
  --[":lb__xingsong"] = "每轮开始时，你可以明置一张手牌且此牌不可被弃置。每回合限一次，其他角色使用牌后，若你有与此牌同名的「<b>星颂</b>」牌，你可以与其各摸一张牌。",
  --10.5
  [":lb__xingsong"] = "每轮开始时，你可以明置一张手牌且此牌不可被弃置、获得且不计入手牌上限。每回合限一次，其他角色使用牌后，若你有与此牌同名的「<b>星颂</b>」牌，你可以与其各摸一张牌。",

  ["@@lb__xingsong-inhand"] = "星颂",
  ["#lb__xingsong"] = "星颂：你可以明置一张手牌，此牌不可被弃置、获得且不计入手牌上限",
  --["#lb__xingsong-give"] = "星颂：你可以交给 %dest 一张 %arg ，然后 %dest 与你各摸一张牌",
  ["#lb__xingsong-ask"] = "星颂：你可以与 %dest 各摸一张牌",

  ["#cancelDismantle"] = "%arg ： %card 不能被弃置",
  ["#cancelPrey"] = "%arg ： %card 不能被获得",

  ["$lb__xingsong1"] = "以此身躯，与你同道，护你左右，领你远行。",
  ["$lb__xingsong2"] = "你所求的，便是你所得的。",
}

skel:addEffect(fk.RoundStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and not player:isKongcheng()
  end,
  on_cost = function(self, event, target, player, data)
    local cards = DIY.askToShowCards(player, {
      min_num = 1,
      max_num = 1,
      skill_name = skel.name,
      prompt = "#lb__xingsong",
      cancelable = true,
    })
    if #cards > 0 then
      event:setCostData(self, { cards = cards })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local card = Fk:getCardById(event:getCostData(self).cards[1])
    player.room:setCardMark(card, "@@lb__xingsong-inhand", 1)
    if #player:getTableMark("@$lb__chuyuan") == 0 then
      player.room:addTableMark(player, "@$lb__chuyuan", card.id)
    end
  end,
})

local xinsong_dis = {
  can_refresh = function(self, event, target, player, data)
    if player.dead then return end
    for _, move in ipairs(data) do
      if move.from == player and (move.moveReason == fk.ReasonDiscard or move.moveReason == fk.ReasonPrey) then
        for _, info in ipairs(move.moveInfo) do
          if Fk:getCardById(info.cardId):getMark("@@lb__xingsong-inhand") > 0 then
            return true
          end
        end
      end
    end
  end,
  ---@param player ServerPlayer
  on_refresh = function(self, event, target, player, data)
    local ids = {}
    for _, move in ipairs(data) do
      if move.from == player and (move.moveReason == fk.ReasonDiscard or move.moveReason == fk.ReasonPrey) then
        for _, info in ipairs(move.moveInfo) do
          if Fk:getCardById(info.cardId):getMark("@@lb__xingsong-inhand") > 0 then
            table.insert(ids, info.cardId)
          end
        end
      end
      if #ids > 0 then
        player.room:sendLog {
          type = move.moveReason == fk.ReasonDiscard and "#cancelDismantle" or "#cancelPrey",
          card = ids,
          arg = "lb__xingsong",
          toast = true,
        }
        player.room:cancelMove(data, ids)
      end
    end
  end,
}

skel:addEffect(fk.BeforeCardsMove, xinsong_dis)
--skel:addEffect(fk.AfterCardsMove, xinsong_dis)

skel:addEffect(fk.CardUseFinished, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target ~= player and player:hasSkill("lb__xingsong") and not target.dead and
        player:usedSkillTimes(skel.name) == 0 and -- 8.30
        table.find(player:getCardIds("h"), function(id)
          local c = Fk:getCardById(id)
          return c:getMark("@@lb__xingsong-inhand") > 0 and c.trueName == data.card.trueName
        end)
  end,
  on_cost = function(self, event, target, player, data)
    --[[
    local cards = table.filter(player:getCardIds("h"),function (id)
      local c = Fk:getCardById(id)
      return c:getMark("@@lb__xingsong-inhand") > 0 and c.trueName == data.card.trueName
    end)
    if #cards == 0 then return end
    local pattern = ".|.|.|.|.|.|"..table.concat(cards,",")
    local cards = player.room:askToCards(player,{
      min_num = 1,
      max_num = 1,
      include_equip = false,
      skill_name = skel.name,
      prompt = "#lb__xingsong-give::"..target.id..":"..Fk:translate(data.card.trueName),
      pattern = pattern,
      cancelable = true,
    })
    if #cards > 0 then
      event:setCostData(self,{cards = cards})
      return true
    end
    --]]
    return player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#lb__xingsong-ask::" .. target.id })
  end,
  on_use = function(self, event, target, player, data)
    --player.room:obtainCard(target, event:getCostData(self).cards, true, fk.ReasonGive, player, "lb__xingsong")
    player:drawCards(1, skel.name)
    target:drawCards(1, skel.name)
  end,
})

skel:addEffect("prohibit", {
  prohibit_discard = function(self, player, card)
    return card:getMark("@@lb__xingsong-inhand") > 0
  end,
})

skel:addEffect("maxcards", {
  exclude_from = function(self, player, card)
    return card:getMark("@@lb__xingsong-inhand") > 0
  end,
})

return skel